package com.weyoung.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.weyoung.admin.dto.req.UserDetailAddDTO;
import com.weyoung.admin.dto.req.UserSetRoleDTO;
import com.weyoung.admin.mapper.UserMapper;
import com.weyoung.admin.service.UserDetailService;
import com.weyoung.admin.mapper.UserDetailMapper;
import com.weyoung.common.model.entity.UserDO;
import com.weyoung.common.model.entity.UserDetailDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static cn.hutool.core.date.DateTime.now;

/**
* @author 86182
* @description 针对表【user_detail(用户详细信息表)】的数据库操作Service实现
* @createDate 2025-04-14 11:44:53
*/
@Service
@Slf4j
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetailDO>
    implements UserDetailService{

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private UserMapper userMapper;



    @Override
    public void add(UserDetailAddDTO requestParam) {

//        Integer id = Integer.parseInt(StpUtil.getLoginId().toString());
//
//        UserDetailDO userdd =  userDetailMapper.selectById(id);
        Integer id = requestParam.getUserId();
        UserDetailDO userdd =  userDetailMapper.selectById(id);
        if(userdd==null){
            //如果没有记录过，就新增
            UserDetailDO userDetailDO = BeanUtil.copyProperties(requestParam, UserDetailDO.class);
            Date now = now();
            userDetailDO.setUserId(id);
            userDetailDO.setUpdateDate(now);
            userDetailDO.setCreateDate(now);
            userDetailMapper.insert(userDetailDO);
        }
        else {
            //存在记录，就改动
            Date now = now();
            UpdateWrapper<UserDetailDO> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(UserDetailDO::getUserId, id)
                    .set(UserDetailDO::getPhone, requestParam.getPhone())
                    .set(UserDetailDO::getEmail, requestParam.getEmail())
                    .set(UserDetailDO::getRealName, requestParam.getRealName())
                    .set(UserDetailDO::getGender, requestParam.getGender())
                    .set(UserDetailDO::getBirthDate, requestParam.getBirthDate())
                    .set(UserDetailDO::getUpdateDate, now);

            update(updateWrapper);

        }


    }

    @Override
    public void add1(UserDetailAddDTO requestParam) {
        Date now = now();
        UserDetailDO userDetailDO = BeanUtil.copyProperties(requestParam, UserDetailDO.class);
        UserDO userDO = BeanUtil.copyProperties(requestParam, UserDO.class);

        System.out.println("userDO:"+userDO);
        System.out.println("userDetailDO:"+userDetailDO);
        if(userDO.getIsDeleted() == null || userDO.getIsDeleted().equals("")) userDO.setIsDeleted("0");
        userDO.setCreateDate(now);
        userDO.setUpdateDate(now);
        userMapper.insert(userDO);

        if(userDetailDO.getGender() == null || userDetailDO.getGender().equals("")) userDetailDO.setGender("other");
        if(userDetailDO.getIsAlive() == null || userDetailDO.getIsAlive().equals("")) userDetailDO.setIsAlive("1");
        if(userDetailDO.getIsDeleted() == null || userDetailDO.getIsDeleted().equals("")) userDetailDO.setIsDeleted("0");
        userDetailDO.setUpdateDate(now);
        userDetailDO.setCreateDate(now);
        userDetailMapper.insert(userDetailDO);
    }

    @Override
    public void update(UserDetailAddDTO requestParam) {
        Integer id = requestParam.getUserId();
            //存在记录，就改动
        Date now = now();
        UpdateWrapper<UserDetailDO> updateWrapper = new UpdateWrapper<>();
        UserDO userDO = BeanUtil.copyProperties(requestParam, UserDO.class);
        UserDetailDO userDetailDO = BeanUtil.copyProperties(requestParam, UserDetailDO.class);

        updateWrapper.lambda().eq(UserDetailDO::getUserId, id);
        if(userDetailDO.getPhone()!=null && !userDetailDO.getPhone().equals("")) updateWrapper.lambda().set(UserDetailDO::getPhone, userDetailDO.getPhone());
        if(userDetailDO.getEmail()!= null && !userDetailDO.getEmail().equals("")) updateWrapper.lambda().set(UserDetailDO::getEmail, userDetailDO.getEmail());
        if(userDetailDO.getRealName()!= null && !userDetailDO.getRealName().equals("")) updateWrapper.lambda().set(UserDetailDO::getRealName, userDetailDO.getRealName());
        if(userDetailDO.getGender()!= null && !userDetailDO.getGender().equals("")) updateWrapper.lambda().set(UserDetailDO::getGender, userDetailDO.getGender());
        if(userDetailDO.getBirthDate() != null ) updateWrapper.lambda().set(UserDetailDO::getBirthDate, userDetailDO.getBirthDate());
        if(userDetailDO.getAddressInfo() != null && !userDetailDO.getAddressInfo().equals("")) updateWrapper.lambda().set(UserDetailDO::getAddressInfo, userDetailDO.getAddressInfo());
        if(userDetailDO.getProvince() != null && !userDetailDO.getProvince().equals("")) updateWrapper.lambda().set(UserDetailDO::getProvince, userDetailDO.getProvince());
        if(userDetailDO.getAge() != null) updateWrapper.lambda().set(UserDetailDO::getAge, userDetailDO.getAge());
        if(userDetailDO.getIsAlive() != null) updateWrapper.lambda().set(UserDetailDO::getIsAlive, userDetailDO.getIsAlive());
        updateWrapper.lambda().set(UserDetailDO::getUpdateDate, now);
        update(updateWrapper);

        if(requestParam.getFamilyId() != null){
            UpdateWrapper<UserDO> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.lambda().eq(UserDO::getUserId, id);
            if(userDO.getFamilyId() != null && !userDO.getFamilyId().equals("")) updateWrapper1.lambda().set(UserDO::getFamilyId, userDO.getFamilyId());
            if(userDO.getUsername() != null && !userDO.getUsername().equals("")) updateWrapper1.lambda().set(UserDO::getUsername, userDO.getUsername());
            if(userDO.getPassword() != null && !userDO.getPassword().equals("")) updateWrapper1.lambda().set(UserDO::getPassword, userDO.getPassword());
            if(userDO.getRole() != null && !userDO.getRole().equals("")) updateWrapper1.lambda().set(UserDO::getRole, userDO.getRole());
            userMapper.update(null, updateWrapper1);
        }
    }





    @Override
    public void delete(Integer id) {
        UserDetailDO userdd =  userDetailMapper.selectById(id);
        if(userdd==null){
            log.info("删除用户不存在！");
            return;
        }
        Date now = now();
        UserDetailDO userDetailDO = new UserDetailDO();
        UpdateWrapper<UserDetailDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(UserDetailDO::getUserId, id)
                .set(UserDetailDO::getUpdateDate, now) //修改变更时间
                .set(UserDetailDO::getIsDeleted, "1");
        update(updateWrapper);
    }

    @Override
    public boolean updateUserRoleById(UserSetRoleDTO requestParam) {
        UpdateWrapper<UserDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(UserDO::getUserId, requestParam.getId())
                .set(UserDO::getRole, requestParam.getRole());
        return userMapper.update(null, updateWrapper) > 0;
    }


}




